using System.Runtime.CompilerServices;
using UnityEngine;
using UnityEngine.UI;

public class ScrollRectController : MonoBehaviour
{
	public delegate void ScrollRectEventHandler();

	public ScrollRect scrollRect;

	[HideInInspector]
	public RectTransform rectTransform;

	private Vector3[] worldCorners;

	public bool isScrolling;

	private Vector3 contentPosition = Vector3.zero;

	private float scrollTargetPosition;

	private bool scrollToTargetPosition;

	[method: MethodImpl(32)]
	public static event ScrollRectEventHandler ScrollMovedEvent;

	public static void FireScrollMovedEvent()
	{
		if (ScrollRectController.ScrollMovedEvent != null)
		{
			ScrollRectController.ScrollMovedEvent();
		}
	}

	private void Awake()
	{
		scrollRect = GetComponent<ScrollRect>();
		rectTransform = GetComponent<RectTransform>();
		GetComponent<Image>().color = new Color(0f, 0f, 0f, 0f);
	}

	private void Start()
	{
		Object.Destroy(scrollRect.content.GetComponent<Image>());
		Object.Destroy(scrollRect.content.GetComponent<CanvasRenderer>());
	}

	private void LateUpdate()
	{
		if (scrollToTargetPosition)
		{
			scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, scrollTargetPosition, 6f * Time.deltaTime);
			if (Mathf.Abs(scrollRect.horizontalNormalizedPosition - scrollTargetPosition) < 0.01f)
			{
				scrollToTargetPosition = false;
			}
		}
		isScrolling = contentPosition != scrollRect.content.position;
		if (isScrolling)
		{
			contentPosition = scrollRect.content.position;
			FireScrollMovedEvent();
		}
	}

	private Vector3[] GetCorners(bool recalculate = false)
	{
		if (recalculate || worldCorners == null)
		{
			worldCorners = new Vector3[4];
			rectTransform.GetWorldCorners(worldCorners);
		}
		return worldCorners;
	}

	private void ScrollHorizontalTo(float to)
	{
		scrollRect.horizontalNormalizedPosition = to;
	}

	private float GetScrollPositionForChild(RectTransform childRect, bool closestVisible = false)
	{
		float x = scrollRect.content.transform.localScale.x;
		Vector3[] array = new Vector3[4];
		rectTransform.GetLocalCorners(array);
		float num = array[2].x - array[0].x;
		Vector3[] array2 = new Vector3[4];
		scrollRect.content.GetLocalCorners(array2);
		float num2 = array2[2].x - array2[0].x;
		Vector3[] array3 = new Vector3[4];
		childRect.GetLocalCorners(array3);
		float num3 = array3[2].x - array3[0].x;
		float num4 = childRect.localPosition.x + num3 * (0.5f - childRect.pivot.x);
		float value = (num4 * x - num / 2f) / (num2 * x - num);
		if (closestVisible)
		{
			value = (num4 * x + num3 * x / 2f - num / 2f) / (num2 * x - num) - num / 2f / (num2 * x - num);
		}
		return Mathf.Clamp(value, 0f, 1f);
	}

	public void ScrollHorizontalToChild(RectTransform childRect, bool closestVisible = false)
	{
		ScrollHorizontalTo(GetScrollPositionForChild(childRect, closestVisible));
	}

	public void ScrollHorizontalLerpToChild(RectTransform childRect, bool closestVisible = false)
	{
		scrollTargetPosition = GetScrollPositionForChild(childRect, closestVisible);
		scrollToTargetPosition = true;
	}

	public bool IsItemVisible(RectTransform item)
	{
		Vector3[] corners = GetCorners();
		Vector3[] array = new Vector3[4];
		item.GetWorldCorners(array);
		if (array[0].x > corners[2].x)
		{
			return false;
		}
		if (array[2].x < corners[0].x)
		{
			return false;
		}
		return true;
	}
}
